package com.kedou.app;

import java.util.ArrayList;

import com.kedou.adapter.FollowedProjectAdapter;
import com.kedou.domain.ProjectItem;
import com.kedou.domain.User;
import com.kedou.net.PostFollowedProject;
import com.kedou.net.PostUserInfo;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 关注 界面
 * 
 * @author zhouchao
 * 
 */
public class AttentionActivity extends BaseActivity implements
		OnItemClickListener {
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgSetting;
	private ListView mListview;
	private FollowedProjectAdapter mAdapter;
	private ArrayList<ProjectItem> mProjects;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_attention);
		initView();
		initListview();
		getDataFromServer();
		setListener();
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.title_attention);
		mImgSetting = (ImageView) findViewById(R.id.img_setting);
		mImgSetting.setVisibility(View.VISIBLE);
		mListview = (ListView) findViewById(R.id.listview);
	}

	/**
	 * 初始化listview
	 */
	private void initListview() {
		mProjects = new ArrayList<ProjectItem>();
		mAdapter = new FollowedProjectAdapter(AttentionActivity.this, mProjects);
		mListview.setAdapter(mAdapter);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mListview.setOnItemClickListener(this);
	}

	/**
	 * 从服务端 获取数据
	 */
	private void getDataFromServer() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(AttentionActivity.this)) {
			new PostFollowedProject(
					PreferencesUtil.getString(AttentionActivity.this, "token"),
					PreferencesUtil.getString(AttentionActivity.this, "userId"),
					Config.SOURCE, Util.getImei(AttentionActivity.this),
					new PostFollowedProject.SuccessCallback() {

						@Override
						public void onSuccess(String msg,
								ArrayList<ProjectItem> projects) {
							mProgressDialog.dismiss();
							mAdapter.addList(projects);
						}
					}, new PostFollowedProject.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(AttentionActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, AttentionActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	}
}
